package com.nc.delivery_project.server.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import com.nc.delivery_project.server.domain.City;
import com.nc.delivery_project.server.domain.Department;


public class DepartmentDao extends AbstractDao<Department> {

	protected final static String ENTITY_NAME = Department.class.getSimpleName();
	
	protected final static String DEPT_ID = "DEPT_ID";
	protected final static String ADDRESS= "ADDRESS";
	protected final static String POSTAL_CODE = "POSTAL_CODE";
	protected final static String CITY_ID = "CITY_ID";
	
		 
	public DepartmentDao(Connection connection) throws SQLException {
		super(connection,ENTITY_NAME);
	}
	 
	
	public DepartmentDao() {
		super(ENTITY_NAME);
	}

	@Override
	protected void prepareInsertStatement(CallableStatement statement,
			Department object) throws SQLException {
		statement.setInt(1, object.getCity().getCityId());
		statement.setString(2, object.getAddress()+" "+object.getPostalCode());
	    statement.registerOutParameter(3, Types.NUMERIC);
	    statement.setString(4, object.getAddress());
	    statement.setString(5, object.getPostalCode());
		
	}
	@Override
	protected void prepareUpdateStatement(CallableStatement statement,
			Department object) throws SQLException {
		statement.setInt(1, object.getDeptId());
	    statement.setInt(2, object.getCity().getCityId());
	    statement.setString(3,  object.getAddress()+" "+object.getPostalCode());
	    statement.setString(4, object.getAddress());
	    statement.setString(5, object.getPostalCode());
		
	}
	@Override
	protected List<Department> parseResultSet(ResultSet rs) throws SQLException {
		// парсинг департаментов
		List<Department> depts = new ArrayList<>();
		
		 while (rs.next()) {
			 int deptId = rs.getInt(DEPT_ID);
			 String address = rs.getString(ADDRESS);
			 String postalCode = rs.getString(POSTAL_CODE);
			 int cityId = rs.getInt(CITY_ID);

			 City city = new City(cityId);
			 depts.add(new Department(deptId, address, postalCode, city));

		 }


		return depts;
	}
	
	 @Override
	    public int create(Department obj) throws SQLException {
	        try (CallableStatement statement = connection.prepareCall(getInsertQuery())) {
	            prepareInsertStatement(statement, obj);
	            statement.executeUpdate();
	         
	            return statement.getInt(3);
	        }
	    }


	public List<Department> readByCityId(int cityId) throws SQLException {
		 try (PreparedStatement statement = connection.prepareStatement(getSelectByCityIdQuery())) {
	            statement.setInt(1, cityId);
	            ResultSet rs = statement.executeQuery();
	            return parseResultSet(rs);
	        }
	}
	
	protected String getSelectByCityIdQuery() {
	     return queryManager.getQuery(entityName, "read_by_city_id");        
	}
	
}